layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * APP评论管理
     */
    var NanAppReview = {
        tableId: "nanAppReviewTable"
    };

    // 加载评分下拉框
    var enumAjax = new $ax(Feng.ctxPath + "/enums/reviewRating");
    var enumRes = enumAjax.start();
    var ratings = enumRes.data;
    var html = "";
    for(var key in ratings){
        html += '<option value="'+key+'">'+ratings[key]+'</option>'
    }
    $("#rating").append(html);
    form.render('select');

    /**
     * 初始化表格的列
     */
    NanAppReview.initColumn = function () {
        return [[
            {field: 'userId', sort: true, title: '用户ID'},
            {field: 'userEmail', sort: false, title: '用户邮箱', minWidth: 180},
            {field: 'ratingStr', sort: false, title: '评分'},
            {field: 'content', sort: false, title: '评论内容', minWidth: 200},
            {field: 'os', sort: false, title: '操作系统'},
            {field: 'deviceModel', sort: false, title: '设备型号', minWidth: 150},
            {field: 'createTime', sort: true, title: '评论时间', minWidth: 160},
        ]];
    };

    /**
     * 点击查询按钮
     */
    NanAppReview.search = function () {
        var queryData = {};
        queryData['email'] = $('#email').val();
        queryData['rating'] = $('#rating').val();
        table.reload(NanAppReview.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + NanAppReview.tableId,
        url: Feng.ctxPath + '/nanAppReview/list',
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: NanAppReview.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        NanAppReview.search();
    });

    // 排序事件
    table.on('sort('+NanAppReview.tableId+')', function(obj){
        table.reload(NanAppReview.tableId, {
            initSort: obj,
            where: {
                field: obj.field.replace(/([A-Z])/g,"_$1").toLowerCase(),
                order: obj.type
            }
        });
    });
});
